/**
 *
 */
package com.vivi.codereview.displayer;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import com.vivi.codereview.WhatTheFuckException;
import com.vivi.codereview.tools.Log;

/**
 * Take a file and display it.
 *
 * @author nvillard
 *
 */
public class Displayer {

  private String fullpathfile;

  public Displayer(DisplayerEnum fileEnum) throws Exception {
    this.fullpathfile = fileEnum.filename;

  }

  public int display() throws Exception {
    String file = readFile(fullpathfile);
    Log.log(file, Log.I);
    return Log.Exit.OK;
  }

  protected String readFile(String file) throws Exception {
    String content = null;
    InputStreamReader isr = null;
    try {
      isr = new InputStreamReader(getClass().getResourceAsStream(file));
      BufferedReader reader = new BufferedReader(isr);
      StringBuilder buffer = new StringBuilder();
      for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        buffer.append(line);
        buffer.append("\n");
      }
      content = buffer.toString();
    }
    catch (FileNotFoundException e) {
      Log.log("File not found " + file, Log.E);
      throw new WhatTheFuckException(Log.Exit.FILE_NOT_EXIST);
    }
    catch (IOException e) {
      Log.log("IOExeption while reading " + file, Log.E);
      throw new WhatTheFuckException(Log.Exit.IO_EXCEPTION);
    }
    finally {
      if (isr != null) {
        isr.close();
      }
    }
    return content;
  }

  public enum DisplayerEnum {

    Help("help.txt"),

    Release("release.txt");

    private String filename;

    private DisplayerEnum(String filename) {
      this.filename = filename;
    }

  }

}
